#include "stdafx.h"
#include "ControlExt.h"


// CControlExt
CControlExt::CControlExt()
{
	m_nFontSize = 10;
	m_nFontUnit = UnitPoint;
	m_nFontStyle = FontStyleRegular;
	m_pFontFamily = new FontFamily(L"Arial");
	m_pFont=new Gdiplus::Font(m_pFontFamily,(REAL)m_nFontSize, m_nFontStyle, m_nFontUnit);

	m_pBrush = new SolidBrush(Color(255,255,255,255));
	m_nStringFormat.SetAlignment(StringAlignmentNear);
	//m_nStringFormat.SetLineAlignment(StringAlignmentCenter);

	m_pMemBgDC = NULL;
}

CControlExt::~CControlExt()
{
	//release font and brush
	if(m_pFontFamily!=NULL) { delete m_pFontFamily;  m_pFontFamily=NULL; }
	if(m_pFont!=NULL) { delete m_pFont;  m_pFont=NULL; }
	if(m_pBrush!=NULL) { delete m_pBrush;  m_pBrush=NULL; }
}

/********************************************************************
 ********************************************************************
               Set Font and Brush
********************************************************************
********************************************************************/
BOOL CControlExt::SetFontSize(int nFontSize)
{
    /*CDC* tmpDC = GetDC(); 
    int dpi = GetDeviceCaps(tmpDC->GetSafeHdc(), LOGPIXELSX);
	ReleaseDC(tmpDC);

	int tmpSize = (int)(nFontSize*(96.0/dpi));*/
	int tmpSize = (int)(nFontSize*(96.0/80));
	if(m_nFontSize != tmpSize) {
		m_nFontSize = tmpSize;
		if(m_pFont) { delete m_pFont;  m_pFont = NULL; }
		m_pFont = new Gdiplus::Font(m_pFontFamily, (REAL)m_nFontSize, m_nFontStyle, m_nFontUnit);
	}
	return TRUE;
}

BOOL CControlExt::SetFontStyle(int nFontStyle)
{
	if(m_nFontStyle != nFontStyle) {
		m_nFontStyle = nFontStyle;
		if(m_pFont) { delete m_pFont;  m_pFont = NULL; }
		m_pFont = new Gdiplus::Font(m_pFontFamily, (REAL)m_nFontSize, m_nFontStyle, m_nFontUnit);
	}
	return TRUE;
}

BOOL CControlExt::SetFontSizeUnit(Unit nFontUnit)
{
	if(m_nFontUnit != nFontUnit) {
		m_nFontUnit = nFontUnit;
		if(m_pFont) { delete m_pFont;  m_pFont = NULL; }
		m_pFont = new Gdiplus::Font(m_pFontFamily, (REAL)m_nFontSize, m_nFontStyle, m_nFontUnit);
	}
	return TRUE;
}

BOOL CControlExt::SetFontFamily(CString strFontFamily)
{
	if(m_pFontFamily) { delete m_pFontFamily;  m_pFontFamily = NULL; }

	m_pFontFamily = new FontFamily(strFontFamily);
	if(FontFamilyNotFound == m_pFontFamily->GetLastStatus()) {
		delete m_pFontFamily;  m_pFontFamily=NULL;
		m_pFontFamily = new FontFamily(_T("Arial"));
	}

	if(m_pFont) { delete m_pFont;  m_pFont = NULL; }
	m_pFont = new Gdiplus::Font(m_pFontFamily, (REAL)m_nFontSize, m_nFontStyle, m_nFontUnit);
	return TRUE;
}

BOOL CControlExt::SetFontColor(int nRed,int nGreen, int nBule)
{
	if(m_pBrush) { delete m_pBrush;  m_pBrush = NULL; }
	m_pBrush=new SolidBrush(Color(255,nRed,nGreen,nBule));
	return TRUE;
}

void CControlExt::SetAlignFormat(StringAlignment align)
{
	m_nStringFormat.SetAlignment(align);
}

void CControlExt::SetLineAlignFormat(StringAlignment align)
{
	m_nStringFormat.SetLineAlignment(align);
}